package com.lincoln.service;

import com.lincoln.bean.ApiCode;
import com.lincoln.bean.ApiCodeException;
import com.lincoln.entity.permission.DbPermission;
import com.lincoln.entity.role.DbRole;
import com.lincoln.repository.PermissionRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class PermissionService extends BaseService<DbPermission, PermissionRepository> {
    /**
     * 建立分页排序请求
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageRequest buildPageRequest(int pageNo, int pageSize, Sort.Order... orders) {
        Sort sort = Sort.by(orders);
        return PageRequest.of(pageNo, pageSize, sort);
    }

    public Predicate createPredicate(Root<DbPermission> root, CriteriaBuilder cb, Map<String, Object> params) {
        Predicate result = cb.and();
        if (params.get("name") != null) {
            Predicate p = cb.equal(root.get("name").as(String.class), params.get("name"));
            result = cb.and(result, p);
        }
        return result;
    }

    /**
     * 批量保存Permission
     *
     * @param names
     * @throws ApiCodeException
     */
    public void savePermissions(String[] names) throws ApiCodeException {
        for (String name : names) {
            DbPermission p = findByUniqueForOne("name", name);
            if (p != null) {
                throw new ApiCodeException(ApiCode.build(ApiCode.FAIL, "permission(" + name + ") is already existed!"));
            }
            DbPermission permission = new DbPermission();
            permission.setName(name);
            save(permission);
        }
    }

    /**
     * 批量查询id(查出一个不存在就抛异常)
     *
     * @param permissions
     * @return
     * @throws ApiCodeException
     */
    public List<DbPermission> findByIds(String permissions) throws ApiCodeException {
        String[] ps = permissions.split(",");
        List<DbPermission> dbPermissions = new ArrayList<>();
        for (String p : ps) {
            DbPermission permission = findByIdOrName(p);
            if (permission == null) {
                throw new ApiCodeException(ApiCode.build(ApiCode.PERMISSION_NONE));
            } else {
                dbPermissions.add(permission);
            }
        }
        return dbPermissions;
    }

    /**
     * 通过等级id或名称查询权限
     *
     * @param idOrName
     * @return
     */
    public DbPermission findByIdOrName(String idOrName) {
        DbPermission permission = null;
        try {
            long id = Long.parseLong(idOrName);
            permission = findById(id);
            if (permission == null) {
                throw new Exception("可能不是id,在这抛个异常让下面catch");
            }
        } catch (Exception e) {
            permission = findByUniqueForOne("name", idOrName);
        }
        return permission;
    }
}
